package jgl.vajra.driver.part.vehicle.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.driver.R;
import jgl.vajra.driver.bean.AddressBean;
import jgl.vajra.driver.bean.PeripheralPoiBean;
import jgl.vajra.driver.databinding.ActivityPeripheralPoiBinding;
import jgl.vajra.driver.event.CityPickerEvent;
import jgl.vajra.driver.event.WorkingFieldEvent;
import jgl.vajra.driver.manager.IntentManager;
import jgl.vajra.driver.part.vehicle.contract.PeripheralPoiContract;
import jgl.vajra.driver.part.vehicle.viewModel.PeripheralPoiViewModel;
import jgl.vajra.driver.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.driver.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.driver.widget.manager.SPManager;
import jgl.vajra.driver.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.driver.widget.mvvm.view.BaseMVVMActivity;

@CreateViewModel(PeripheralPoiViewModel.class)
public class PeripheralPoiActivity extends BaseMVVMActivity<PeripheralPoiViewModel, ActivityPeripheralPoiBinding> implements PeripheralPoiContract.View, BaseBindingItemPresenter<PeripheralPoiBean> {

    private PoiSearch mPoiSearch;
    private String address;

    private List<PeripheralPoiBean> list = new ArrayList<>();
    private SingleTypeBindingAdapter<PeripheralPoiBean> adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_peripheral_poi;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        //位置信息
        AddressBean addressData = SPManager.HomeData.getAddressData();

        if (addressData != null && !TextUtils.isEmpty(addressData.locality)) {
            address = addressData.locality.replace("市", "");
            mBinding.tvAddress.setText(address);
        }

        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter<>(this, list, R.layout.item_peripheral_poi);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        //创建POI检索实例
        mPoiSearch = PoiSearch.newInstance();
        OnGetPoiSearchResultListener listener = new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                List<PoiInfo> allPoi = poiResult.getAllPoi();
                if (allPoi != null && allPoi.size() != 0) {
                    list.clear();
                    for (int i = 0; i < allPoi.size(); i++) {
                        list.add(new PeripheralPoiBean(allPoi.get(i).name, allPoi.get(i).address, allPoi.get(i).location.latitude, allPoi.get(i).location.longitude));
                    }
                    adapter.refresh();
                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }

            //废弃
            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }
        };

        //设置检索监听器
        mPoiSearch.setOnGetPoiSearchResultListener(listener);


        mBinding.etAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                /*
                 *  PoiCiySearchOption 设置检索属性
                 *  city 检索城市
                 *  keyword 检索内容关键字
                 *  pageNum 分页页码
                 */
                mPoiSearch.searchInCity(new PoiCitySearchOption()
                        .city(address) //必填
                        .keyword(editable.toString().trim()) //必填
                        .pageNum(0)
                        .pageCapacity(100));
            }
        });
    }

    //选择位置
    @Override
    public void onTriangle() {
        IntentManager.toCityPickerActivity(this);
    }

    @Override
    public void onFinish() {
        finish();
    }

    @Override
    protected boolean isRegisterEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCityPickerEvent(CityPickerEvent event) {
        address = event.name;
        mBinding.tvAddress.setText(address);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPoiSearch.destroy();
    }

    @Override
    public void onItemClick(int position, PeripheralPoiBean itemData) {
        EventBus.getDefault().post(new WorkingFieldEvent(itemData));
        finish();
    }
}
